﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Lenic.DI.Lifetimes
{
    [DebuggerStepThrough]
    internal sealed class LifetimeCachedKey
    {
        public Type Type { get; private set; }

        public string Name { get; private set; }

        private int hash = 0;

        public LifetimeCachedKey(Type type, string name)
        {
            Type = type;
            Name = name;

            hash = Type.GetHashCode();
            if (name != null)
                hash ^= Name.GetHashCode();
        }

        public override int GetHashCode()
        {
            return hash;
        }
    }

    [DebuggerStepThrough]
    internal sealed class LifetimeCachedKeyEquality : IEqualityComparer<LifetimeCachedKey>
    {
        public bool Equals(LifetimeCachedKey x, LifetimeCachedKey y)
        {
            if (x == null || y == null)
                return false;

            return x.Type.Equals(y.Type) && string.Equals(x.Name, y.Name);
        }

        public int GetHashCode(LifetimeCachedKey obj)
        {
            return obj.GetHashCode();
        }
    }
}